﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Data;

namespace HDK.UI.Controls
{
    public interface IVariableSizedGridViewResizableItem
    {
        int Width { get; set; }
        int Height { get; set; }
    }
    public class VariableSizedGridViewLayoutItem : IVariableSizedGridViewResizableItem
    {
        public int Width { get; set; }
        public int Height { get; set; }
    }


    public class VariableSizedGridView : GridView
    {
        public List<IVariableSizedGridViewResizableItem> Layout = new List<IVariableSizedGridViewResizableItem>() { 
            //new VariableSizedGridViewLayoutItem() { Width = 6, Height = 3},
            //new VariableSizedGridViewLayoutItem() { Width = 3, Height = 1},
            //new VariableSizedGridViewLayoutItem() { Width = 3, Height = 1},
            //new VariableSizedGridViewLayoutItem() { Width = 3, Height = 1},
            //new VariableSizedGridViewLayoutItem() { Width = 3, Height = 2},
            //new VariableSizedGridViewLayoutItem() { Width = 2, Height = 1},
            //new VariableSizedGridViewLayoutItem() { Width = 2, Height = 1},
            //new VariableSizedGridViewLayoutItem() { Width = 2, Height = 1},
            new VariableSizedGridViewLayoutItem() { Width = 2, Height = 2},
            new VariableSizedGridViewLayoutItem() { Width = 1, Height = 1},
            new VariableSizedGridViewLayoutItem() { Width = 1, Height = 1},
            new VariableSizedGridViewLayoutItem() { Width = 1, Height = 1},
            new VariableSizedGridViewLayoutItem() { Width = 1, Height = 2},
        };


        int index = -1;
        protected override void PrepareContainerForItemOverride(DependencyObject element, object item)
        {
            base.PrepareContainerForItemOverride(element, item);  

            var castedElement = element as GridViewItem;
            var data = item;//as IVariableSizedGridViewResizableItem;
            var dataItem = this.ItemsSource as IEnumerable<object>;
            ICollectionView cv = dataItem as ICollectionView;

            //if (data != null)
            {
                //element.SetValue(VariableSizedWrapGrid.ColumnSpanProperty, viewModel.Width);
                //element.SetValue(VariableSizedWrapGrid.RowSpanProperty, viewModel.Height);

                int RowSpan;
                int ColSpan;

                index++;
                //int index = -1;
                //if (cv != null)
                //    index = cv.IndexOf(data);

                if (index >= 0 && index < Layout.Count)
                {

                    ColSpan = (int)Layout[index].Width;
                    RowSpan = (int)Layout[index].Height;

                }
                else
                {
                    ColSpan = (int)1;//LayoutSizes.OtherSmallItem.Width;
                    RowSpan = (int)1;//LayoutSizes.OtherSmallItem.Height;

                }
                VariableSizedWrapGrid.SetRowSpan(castedElement, RowSpan); //data.Height
                VariableSizedWrapGrid.SetColumnSpan(castedElement, ColSpan); //data.Width
            }


            
            //castedElement.SetValue(GridViewItem.HorizontalContentAlignmentProperty, HorizontalContentAlignment);
            //castedElement.SetValue(GridViewItem.VerticalContentAlignmentProperty, VerticalContentAlignment);
            //castedElement.SetValue(GridViewItem.HorizontalAlignmentProperty, HorizontalAlignment.Stretch);
            //castedElement.SetValue(GridViewItem.VerticalAlignmentProperty, VerticalAlignment.Stretch);

            //castedElement.UpdateLayout();

            //castedElement.Loaded += castedElement_Loaded;
            //castedElement.SizeChanged += castedElement_SizeChanged;


            var itemsOwner = GridView.GetItemsOwner(element);
            var itemsControlForCOntainer = GridView.ItemsControlFromItemContainer(element);
            var src = this.ItemsSource as CollectionViewSource;

        }

        protected override bool IsItemItsOwnContainerOverride(object item)
        {
            return base.IsItemItsOwnContainerOverride(item);
        }


        void castedElement_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            GridViewItem me = sender as GridViewItem;

        }

        void castedElement_Loaded(object sender, RoutedEventArgs e)
        {
            GridViewItem me = sender as GridViewItem;

            //throw new NotImplementedException();
        }
    }
}
